//
//  TagDetailViewController.m
//  SimpleTimeDemo
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 王丽珍. All rights reserved.
//

#import "TagDetailViewController.h"
#import "MainDataHelper.h"
#import "UIImageView+WebCache.h"
#import "FragmentFirstTableViewCell.h"
#import "FragmentSecondTableViewCell.h"
#import "FragmentModel.h"
#import "FragmentDetailViewController.h"
#import "MJRefresh.h"

@interface TagDetailViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, copy) NSString *string;
@property (nonatomic, copy) NSString *bodyString;

@property (nonatomic, assign) int pageNum;

@end

@implementation TagDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadData];
    
    [self loadTableView];
    
    //刷新次数
    self.pageNum = 1;
    //刷新操作--第三方
    [self setupRefresh];
}

- (void)setupRefresh {
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pushReload)];
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(downReload)];
}

- (void)pushReload {
    [self loadData];
    [self.tableView.header beginRefreshing];
}

- (void)downReload {
    self.pageNum++;
    [self loadData];
    [self.tableView.footer beginRefreshing];
}

#pragma mark --- 数据解析部分 ---
- (void)loadData
{
    self.string = @"http://api2.pianke.me/timeline/list";
    self.bodyString = [NSString stringWithFormat:@"auth=&client=1&deviceid=4B1DC887-3B93-405F-970B-3B2C4C556E2A&limit=%d&start=0&tag=%@&version=3.0.6", self.pageNum*10, self.tag];
    
    [MainDataHelper postDataWithUrlString:self.string andBodyString:self.bodyString compare:^(id object) {
        self.dataArray = [NSMutableArray array];
        
        NSDictionary *dic = object;
        NSArray *array = dic[@"data"][@"list"];
        for (NSDictionary *dic1 in array) {
            FragmentModel *model = [[FragmentModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.dataArray addObject:model];
        }
        
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
}

#pragma mark --- tableView部分 ---
- (void)loadTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

#pragma mark --- cell赋值部分 ---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FragmentModel *model = self.dataArray[indexPath.row];
    
    if ([model.coverimg isEqualToString:@""]) {
        UINib *nib = [UINib nibWithNibName:@"FragmentFirstTableViewCell" bundle:nil];
        [self.tableView registerNib:nib forCellReuseIdentifier:@"FirstCell"];
        FragmentFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FirstCell" forIndexPath:indexPath];
        
        cell.contentLabel.text = model.content;
        cell.contentLabel.numberOfLines = 3;
        [cell.contentLabel sizeToFit];
        cell.timeLabel.text = model.addtime_f;
        [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:model.userinfo[@"icon"]]];
        cell.nameLabel.text = model.userinfo[@"uname"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }else {
        UINib *nib = [UINib nibWithNibName:@"FragmentSecondTableViewCell" bundle:nil];
        [self.tableView registerNib:nib forCellReuseIdentifier:@"SecondCell"];
        FragmentSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecondCell" forIndexPath:indexPath];
        
        [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:model.userinfo[@"icon"]]];
        cell.nameLabel.text = model.userinfo[@"uname"];
        cell.contentLabel.text = model.content;
        cell.contentLabel.numberOfLines = 3;
        [cell.contentLabel sizeToFit];
        cell.timeLabel.text = model.addtime_f;
        [cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    FragmentModel *model = self.dataArray[indexPath.row];
    if ([model.coverimg isEqualToString:@""]) {
        return 200;
    }else {
        return 400;
    }
}

#pragma mark --- 点击跳转页面 ---
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FragmentModel *model = self.dataArray[indexPath.row];
    FragmentDetailViewController *fragmentDetailVC = [[FragmentDetailViewController alloc] init];
    fragmentDetailVC.contentid = model.contentid;
    [self.navigationController pushViewController:fragmentDetailVC animated:YES];
}














- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
